﻿namespace Fiddler
{
    using System;
    using System.Collections;
    using System.Globalization;
    using System.Windows.Forms;

    internal class ListViewItemComparer : IComparer
    {
        internal bool bAscending;
        public bool bStringCompare;
        private int iCol = 0;
        private ListView lvOwner;

        public ListViewItemComparer(ListView oOwner)
        {
            this.lvOwner = oOwner;
            this.bStringCompare = false;
            this.bAscending = true;
        }

        public int Compare(object x, object y)
        {
            int num = -1;
            ListViewItem item = (ListViewItem) x;
            ListViewItem item2 = (ListViewItem) y;
            if (item.SubItems.Count <= this.iCol)
            {
                num = -1;
            }
            else if (item2.SubItems.Count <= this.iCol)
            {
                num = 1;
            }
            else if (this.bStringCompare)
            {
                num = string.Compare(item.SubItems[this.iCol].Text, item2.SubItems[this.iCol].Text, StringComparison.Ordinal);
            }
            else
            {
                try
                {
                    num = int.Parse(item.SubItems[this.iCol].Text, NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign) - int.Parse(item2.SubItems[this.iCol].Text, NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign);
                }
                catch (Exception exception)
                {
                    FiddlerApplication.DebugSpew(exception.Message + "\n" + item.SubItems[this.iCol].Text + "\n" + item2.SubItems[this.iCol].Text);
                }
            }
            if (!this.bAscending)
            {
                num = -num;
            }
            return num;
        }

        public int Column
        {
            get
            {
                return this.iCol;
            }
            set
            {
                int iCol = this.iCol;
                LVNative.LV_COLUMN lParam = new LVNative.LV_COLUMN();
                if (value == iCol)
                {
                    this.bAscending = !this.bAscending;
                }
                else
                {
                    this.iCol = value;
                    this.bAscending = true;
                    lParam.mask = 1;
                    LVNative.SendLVColMessage(this.lvOwner.Handle, 0x105f, (uint) iCol, ref lParam);
                    lParam.fmt &= 0xfffff9ff;
                    LVNative.SendLVColMessage(this.lvOwner.Handle, 0x1060, (uint) iCol, ref lParam);
                }
                lParam.mask = 1;
                LVNative.SendLVColMessage(this.lvOwner.Handle, 0x105f, (uint) this.iCol, ref lParam);
                if (this.bAscending)
                {
                    lParam.fmt &= 0xfffffdff;
                    lParam.fmt |= 0x400;
                }
                else
                {
                    lParam.fmt &= 0xfffffbff;
                    lParam.fmt |= 0x200;
                }
                LVNative.SendLVColMessage(this.lvOwner.Handle, 0x1060, (uint) this.iCol, ref lParam);
            }
        }
    }
}

